'''
Created on 2020年3月3日

@author: Administrator
'''
from orm.model import Team,User
from view.baseview import BaseView
from common.public import COMMON_TRUE

class TeamView(BaseView):
    def __init__(self):
        pass
    
    def getMembers(self,id):
        """取得团队成员列表"""
        result=[]
        user = User.select().where(User.id==id).first()
        if user.dept.up_id == 0:
            # 总经办取得所有用户
            rows = User.select().where((User.isactive==COMMON_TRUE)).order_by(User.dept.desc())
            for row in rows:
                data = {
                    "id":row.id,
                    "name":row.username,
                   }
                result.append(data )

        else:
            if user.ismanager == COMMON_TRUE:
                # 如果是经理
                rows = User.select().where((User.dept_id==user.dept) & (User.isactive==COMMON_TRUE))
                for row in rows:
                    data = {
                        "id":row.id,
                        "name":row.username,
                       }
                    result.append(data )
            elif user.isleader == COMMON_TRUE: # 如果是主管
                # 再加团队成员
                rows = User.select().where((User.leader_id==id) & (User.isactive==COMMON_TRUE))
                for row in rows:
                    data = {
                        "id":row.id,
                        "name":row.username,
                       }
                    result.append(data )
                
            else:
                # 不是主管
                data = {
                    "id":user.id,
                    "name":user.username,
                   }
                result.append(data )
            
        return super().resultSet(200, "团队成员列表取得成功", result)


if __name__ == '__main__':
    tv = TeamView()
    members = tv.getMembers(2)
    print(members)
